package com.anolesoft.yr.mbus.part.mapper;

import com.anolesoft.yr.mbus.part.entity.PartSafetyQuantityDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/11 16:58
 */
@Mapper
public interface PartSafetyQuantityMapper {
    @Insert("insert into part_safety_quantity(" +
            "objuid," +
            "fac_code," +
            "stk_main_uid," +
            "part_model_uid," +
            "highest_reserve," +
            "lowest_reserve," +
            "warning_quantity," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{stkMainUid,jdbcType=VARCHAR}," +
            "#{partModelUid,jdbcType=VARCHAR}," +
            "#{highestReserve,jdbcType=INTEGER}," +
            "#{lowestReserve,jdbcType=INTEGER}," +
            "#{warningQuantity,jdbcType=INTEGER}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(PartSafetyQuantityDO partSafetyQuantityDO);

    @Update("update part_safety_quantity set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "stk_main_uid = #{stkMainUid,jdbcType=VARCHAR}," +
            "part_model_uid = #{partModelUid,jdbcType=VARCHAR}," +
            "highest_reserve = #{highestReserve,jdbcType=INTEGER}," +
            "lowest_reserve = #{lowestReserve,jdbcType=INTEGER}," +
            "warning_quantity = #{warningQuantity,jdbcType=INTEGER}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR} " +
            "where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(PartSafetyQuantityDO partSafetyQuantityDO);

    @Update("update part_safety_quantity set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "PartSafetyQuantityResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "stk_main_uid", property = "stkMainUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_model_uid", property = "partModelUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "highest_reserve", property = "highestReserve", jdbcType = JdbcType.INTEGER),
            @Result(column = "lowest_reserve", property = "lowestReserve", jdbcType = JdbcType.INTEGER),
            @Result(column = "warning_quantity", property = "warningQuantity", jdbcType = JdbcType.INTEGER),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_model_uid", property = "partModel", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartModelMapper.selectOneById", fetchType = FetchType.EAGER)),
            @Result(column = "stk_main_uid", property = "stkMain", one = @One(select = "com.anolesoft.yr.store.stk.dao.StkMainDao.selectOneById", fetchType = FetchType.LAZY))
    })
    @Select("select " +
            "objuid, fac_code, " +
            "stk_main_uid, part_model_uid, highest_reserve, lowest_reserve, warning_quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_safety_quantity where del_flag = 0 ")
    List<PartSafetyQuantityDO> selectAll();

    @ResultMap("PartSafetyQuantityResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "stk_main_uid, part_model_uid, highest_reserve, lowest_reserve, warning_quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_safety_quantity where del_flag = 0 ")
    List<PartSafetyQuantityDO> selectPageAll(Page<PartSafetyQuantityDO> page);

    @ResultMap("PartSafetyQuantityResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "stk_main_uid, part_model_uid, highest_reserve, lowest_reserve, warning_quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_safety_quantity where del_flag = 0 and id=#{id,jdbcType=VARCHAR}")
    PartSafetyQuantityDO selectOneById(@Param("id") String id);

    @ResultMap("PartSafetyQuantityResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "stk_main_uid, part_model_uid, highest_reserve, lowest_reserve, warning_quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_safety_quantity where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.stkMainUid !=null and o.stkMainUid != '' \">" +
            "   and stk_main_uid = #{o.stkMainUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartSafetyQuantityDO> selectList(@Param("o") PartSafetyQuantityDO partSafetyQuantityDO);

    @ResultMap("PartSafetyQuantityResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "stk_main_uid, part_model_uid, highest_reserve, lowest_reserve, warning_quantity," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_safety_quantity where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.stkMainUid !=null and o.stkMainUid != '' \">" +
            "   and stk_main_uid = #{o.stkMainUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartSafetyQuantityDO> selectPageList(@Param("o") PartSafetyQuantityDO partSafetyQuantityDO, Page<PartSafetyQuantityDO> page);
}
